﻿using Lightworks.Core.Constant;
using Lightworks.System.Dto;
using Lightworks.System.Models;
using Lightworks.System.Services;
using Microsoft.AspNetCore.Mvc;

namespace Lightworks.Api.Controllers
{
    [ApiController]
    [Route("api/system/dic")]
    public class DicController : ApiBaseController
    {
        IDictService service;

        public DicController(IDictService service)
        {
            this.service = service;
        }

        //获取字典树
        [HttpGet("tree")]
        public ApiResult Tree()
        {
            var data = service.GetAll();
            return Ok(data);
        }

        //字典明细
        [HttpGet("list")]
        public ApiResult List([FromQuery] DictSearchReq req)
        {
            var data = service.GetDetail(req);
            return Ok(data);
        }

        [HttpPost("save")]
        public ApiResult Save(SysDict model) 
        {
            if (service.SaveDict(model))
            {
                return Ok();
            }
            else {
                return Error("创建失败");
            }
        }

        [HttpPost("saveDetail")]
        public ApiResult SaveDetail(SysDictDetail model)
        {
            if (service.SaveDetail(model))
            {
                return Ok();
            }
            else
            {
                return Error("创建失败");
            }
        }

        [HttpPost("delete/{id}")]
        public ApiResult Delete(int id) 
        {
            if (service.DeleteDict(id))
                return Ok();
            else
                return Error();
        }

        [HttpPost("deleteDetail")]
        public ApiResult DeleteDetail(long[] ids)
        {
            var param = new dynamic[ids.Length];
            for (int i = 0; i < ids.Length; i++) 
            {
                param[i] = ids[i];
            }
            if (service.DeleteDetails(param))
                return Ok();
            else
                return Error();
        }
    }
}
